package com.device.controller;

import com.device.domain.Station;
import com.device.service.IStationService;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.core.web.page.TableDataInfo;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import com.share.common.security.utils.EntityAuditable.EntityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Tag(name = "站点接口管理")
@RestController
@RequestMapping("/station")
public class StationController extends BaseController {


    @Autowired
    private IStationService stationService;


    /**
     * 查询站点列表
     */
    @Operation(summary = "查询站点列表")
    @RequiresPermissions("device:station:list")
    @GetMapping("/list")
    public TableDataInfo list(Station station) {
        startPage();
        List<Station> list = stationService.selectStationList(station);
        return getDataTable(list);
    }


    /**
     * 新增站点
     */
    @Log(title = "站点管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("device:station:add")
    @Operation(summary = "新增站点")
    @PostMapping
    public AjaxResult add(@RequestBody Station station) {
        return toAjax(stationService.saveStation(station));
    }


    /**
     * 修改站点
     */
    @Log(title = "站点管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("device:station:edit")
    @Operation(summary = "修改站点")
    @PutMapping
    public AjaxResult edit(@RequestBody Station station) {
        return toAjax(stationService.updateStation(station));
    }


    /**
     * 删除站点
     */
    @Log(title = "站点管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("device:station:remove")
    @Operation(summary = "删除站点")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        boolean removeBatchByIds = stationService.removeBatchByIds(idList);
        if (removeBatchByIds){
            //设置更新信息
            List<Station> list = idList.stream().map(id -> {
                Station station = new Station();
                station.setId(id);
                EntityUtils.setUpdateInfo(station);
                return station;
            }).toList();
            stationService.updateBatchById(list);
        }
        return toAjax(removeBatchByIds);
    }


    /**
     * 获取站点详细信息
     */
    @Operation(summary = "获取站点详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(stationService.getById(id));
    }


    /**
     * 站点设置关联数据
     */
    @Operation(summary = "站点设置关联数据")
    @PostMapping("/setData")
    public AjaxResult setData(@RequestBody Station station) {
        return toAjax(stationService.setData(station));
    }

}
